﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;


namespace Nirvana
{
    [CustomEditor(typeof(AssetBundleNameImportRule), true)]
    public class AssetBundleNameImportRuleEditor : Editor
    {
        private SerializedProperty includeRegexs;
        private SerializedProperty excludeRegexs;
        private SerializedProperty assetBundleNameFormat, ingoreExtension, clearBundleName;


        public override void OnInspectorGUI()
        {
            base.serializedObject.Update();

            GUILayout.Label("匹配规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents();
            GUI.color = Color.yellow;
            EditorGUILayout.HelpBox("注意：规则文件只会检测同一父目录或同一级目录下的资源。\n\n" +
                "正则例子：1.匹配非空字符：\\S , 2.任意字符：. ", MessageType.None);
            GUI.color = Color.white;
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.includeRegexs);
            EditorGUILayout.PropertyField(this.excludeRegexs);

            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            //说明
            GUILayout.Label("AssetBundle包名规则", GUITheme.LabelTitleStyle);
            GUI.color = Color.yellow;
            EditorGUILayout.HelpBox("注意: AB包名只检测Resources目录下的资源\n" +
                "因此，资源必须放到Resources目录下，以便自动设置AB包名", MessageType.Info);
            GUI.color = Color.white;
            EditorGUILayout.Space();

            GUILayoutEx.BeginContents(GUI.skin.box);
            EditorGUILayout.PrefixLabel("AssetBundle Name Format");
            EditorGUILayout.HelpBox("设置AssetBunlde包名规则\n" +
                "说明：\n" +
                "{filename}：tag记录文件名，{firstdir}：配置表下的第一个子目录开始，{seconddir}，{thirddir}。\n" +
                "一般设置四级目录Tag做AssetBundleName即可，最多就记录到四级目录,避免细分太多AB。\n" +
                "例子：{firstdir}/{seconddir}/{thirddir}/{fourthdir}。\n", MessageType.None);
            EditorGUILayout.PropertyField(this.assetBundleNameFormat);
            GUILayoutEx.EndContents();

            EditorGUILayout.Space();


        
            EditorGUILayout.PrefixLabel("设置忽略哪些后缀文件资源不打包");
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(this.ingoreExtension);
            EditorGUILayout.Space();

            EditorGUILayout.PrefixLabel("清除BundleName");

            EditorGUILayout.PropertyField(this.clearBundleName);
            if (this.clearBundleName.boolValue)
            {
                if (GUILayoutEx.ActionButton("清除当前目录下所有BundleName(多点几次清除)"))
                {
                    var rule = target as AssetBundleNameImportRule;
                    rule.ClearCurDirAssetBundleName();
                }

                if (GUILayoutEx.ActionButton("清除项目内所有BundleName(多点几次清除)"))
                {
                    var rule = target as AssetBundleNameImportRule;
                    rule.ClearAllAssetBundleName();
                }

            }
            EditorGUILayout.Space();

            base.serializedObject.ApplyModifiedProperties();
        }
        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.includeRegexs = serializedObject.FindProperty("includeRegexs");
            this.excludeRegexs = serializedObject.FindProperty("excludeRegexs");
            this.ingoreExtension = serializedObject.FindProperty("ingoreExtension");
            this.assetBundleNameFormat = serializedObject.FindProperty("assetBundleNameFormat");
            this.clearBundleName = serializedObject.FindProperty("clearBundleName");

        }

    }
}


